﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for Course
/// </summary>
public class Course : IReview
{
    private string reviewFolder;

    public string Title { get; set; }
    public string Description { get; set; }
    public string Rating { get; set; }
    public string Content { get; set; }
    public string ReviewFile { get; set; }


    public static string REVIEW_FOLDER;

	public Course()
	{

	}

    public Course(string title, string description, string content, string rating, string reviewFile)
    {
        this.Title = title;
        this.Description = description;
        this.Rating = rating;
        this.Content = content;
        this.ReviewFile = reviewFile;
    }

    public string ReviewFolder
    {
        set { this.reviewFolder = value; }
        get { return string.Format("{0}\\{1}", REVIEW_FOLDER, this.reviewFolder); }
    }

    public void CreateReview()
    {
        try
        {
            CreateReviewFolder();
            CreateReviewFile(this.Title, this.Description, this.Content, this.Rating, this.ReviewFile);
        }
        catch (Exception ex) { }
    }

    public void CreateReview(string title, string description, string content, string rating, string reviewFile)
    {
        try
        {
            CreateReviewFolder();
            CreateReviewFile(title, description, content, rating, reviewFile);
        }
        catch (Exception ex) { }
    }

    private void CreateReviewFolder()
    {
        if (!Directory.Exists(REVIEW_FOLDER))
            Directory.CreateDirectory(REVIEW_FOLDER);
    }

    private void CreateReviewFile(string title, string description,
                                    string content, string rating, string reviewFile )
    {
        if (!File.Exists(reviewFile))
        {
            using (StreamWriter writer = new StreamWriter(reviewFile))
            {
                writer.WriteLine(title);
                writer.WriteLine(rating);
                writer.WriteLine(description);
                writer.WriteLine(content);
            }
        }
    }

    public string GetReviewContent(string reviewFile)
    {
        string content = null;

        try
        {
            using (StreamReader reader = new StreamReader(reviewFile))
            {
                content = reader.ReadToEnd();
            }
        }
        catch (Exception ex) { }

        return content;
    }

    public Course GetCourseObject(string reviewFile)
    {
        Course course = null;
        try
        {
            course = new Course();
            course.ReviewFile = reviewFile;
            using (StreamReader reader = new StreamReader(reviewFile))
            {
                course.Title = reader.ReadLine();
                course.Description = reader.ReadLine();
                course.Rating = reader.ReadLine();
                while (!reader.EndOfStream)
                    course.Content += reader.ReadLine();
            }
        }
        catch (Exception ex) { }

        return course;
    }

    private string ConvertFileToUrl(string file)
    {
        string newFileName = "";
        string[] tokens = file.Split(':');
        tokens[0] = "file:";
        foreach (string item in tokens)
            newFileName += item;

        return newFileName;
    }

    public string GetTitle()
    {
        return this.Title;
    }

    public string GetDescription()
    {
        return this.Description;
    }

    public string GetRating()
    {
        return this.Rating;
    }

    public string GetReviewFile()
    {
        return this.Rating;
    }


    public string GetContent()
    {
        return this.Content;
    }



    public List<Course> GetAllReviews()
    {
        List<Course> reviews = new List<Course>();

        try
        {
            CreateReviewFolder();   // creates folder if it doesn't exist

            String[] files = Directory.GetFiles(ReviewFolder);

            foreach (String file in files)
            {
                Course course = GetCourseObject(file);
                reviews.Add(course);
            }
        }
        catch (Exception ex) { }

        return reviews;
    }


    List<IReview> IReview.GetAllReviews()
    {
        throw new NotImplementedException();
    }
}